package com.wh.service.impl;

import com.wh.beans.LoginInfoCookie;
import com.wh.beans.TreeModel;
import com.wh.dao.BaseDao;
import com.wh.dao.ext.DailyNotebookGroupDao;
import com.wh.dao.ext.DailyNotebookDao;
import com.wh.model.BaseModel;
import com.wh.model.DailyNotebookGroup;
import com.wh.model.DailyNotebook;
import com.wh.service.IExt.IDailyNotebookGroupService;
import com.wh.service.IExt.IDailyNotebookService;
import com.legend.utils.*;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/*
 *author:徐洪昌
 *blog:http://cnblogs.com/xusir
 *datetime:2014-04-27 13:55:58
 */
public class DailyNotebookService extends BaseServiceImp implements IDailyNotebookService {

	Logger log = Logger.getRootLogger();
    private DailyNotebookService() {
    }

    //volatile修饰的写变量不能和之前的读写代码调整，读变量不能和之后的读写代码调整
    private volatile static IDailyNotebookService instance = null;

    /**
     * 单例
     *
     * @return
     */
    public static IDailyNotebookService getInstance() {
        if (instance == null) {
            synchronized (IDailyNotebookService.class) {
                if (instance == null) {
                    instance = new DailyNotebookService();
                }
            }
        }
        return instance;
    }
	/**
	 * 批量删除数据
	 */
	public ReturnResult delByIds(String ids) {
		SqlSession session = getSqlSession();
		ReturnResult result = new ReturnResult();
		try {
			DailyNotebookDao dao = session.getMapper(DailyNotebookDao.class);
			result = delByMap(dao, ids);
		} catch (Exception ex) {
			log.error(ex.getMessage());
		} finally {
			session.close();
		}
		return result;
	}
	@Override
	public ReturnResult delByModel(BaseModel model) {
		SqlSession session = getSqlSession();
		ReturnResult result = new ReturnResult();
		try {
			DailyNotebookDao dao = session.getMapper(DailyNotebookDao.class);
			Integer i = dao.delByModel(model);
			result.setData(i);
			result.setStatus(Status.SUCCESS);
			result.setMsg(i18nUtils.getVal("100001000"));
        } catch (Exception ex) {
			log.error(ex.getMessage());
		} finally {
			session.close();
		}
		return result;
	}
	/**
	 * 分页数据查询
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @param map
	 * @return
	 */
	public ReturnResult selectByMap(int pageIndex, int pageSize, Map<String, Object> map) {
		SqlSession session = getSqlSession();
		DailyNotebookDao dao = session.getMapper(DailyNotebookDao.class);
		int recordNum = (pageIndex - 1) * pageSize;
		List<DailyNotebook> lst = session.selectList(ConfigUtils.getInstance().getDaoPackage(".DailyNotebookDao.selectByMap"), map,
				new RowBounds(recordNum, pageSize));
		int total = dao.count(map);
		ReturnResult result = new ReturnResult();
		result.setMsg(i18nUtils.getVal("100001000"));
                result.setRows(lst);
		result.setTotal(total);
		result.setStatus(Status.SUCCESS);
		session.close();
		return result;
	}

	@Override
	public ReturnResult insertByModel(BaseModel model) {
		SqlSession session = getSqlSession();
		DailyNotebookDao dao = session.getMapper(DailyNotebookDao.class);
		ReturnResult result = insertByModel(dao, model);
		session.close();
		return result;
	}

	@Override
	public ReturnResult editByModel(BaseModel model) {
		SqlSession session = getSqlSession();
		DailyNotebookDao dao = session.getMapper(DailyNotebookDao.class);
		ReturnResult result = editByModel(dao, model);
		session.close();
		return result;
	}

	/**
	 * 根据ID获取一条数据
	 * 
	 * @param id
	 * @return
	 */
	public ReturnResult selectById(String id) {
		SqlSession session = getSqlSession();
		ReturnResult result = new ReturnResult();
		result.setStatus(Status.ERROR);
		BaseDao dao = session.getMapper(DailyNotebookDao.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<DailyNotebook> lst = dao.selectByMap(map);
		if (lst.size() == 1) {
			result.setData(lst.get(0));
			result.setStatus(Status.SUCCESS);
		} else {
			result.setMsg(i18nUtils.getVal("100004000"));
        }
		session.close();
		return result;
	}

	/**
	 * 批量审核数据
	 * 
	 */
	public ReturnResult audit(int state, String[] idArr) {
		SqlSession session = getSqlSession();
		DailyNotebookDao dao = session.getMapper(DailyNotebookDao.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("idArr", idArr);
		map.put("state", state);
		Integer num = dao.audit(map);
		ReturnResult result = new ReturnResult();
		if (num > 0) {
			result.setMsg(i18nUtils.getVal("100001000"));
                    result.setStatus(Status.SUCCESS);
		}
		session.close();
		return result;
	}

	/**
	 * 排序
	 * 
	 * @param sorts
	 * @param ids
	 * @return
	 */
	public ReturnResult sort(String sorts, String ids) {
		ReturnResult result;
		SqlSession session = getSqlSession();
		DailyNotebookDao dao = session.getMapper(DailyNotebookDao.class);
		result = sort(session, dao, sorts, ids);
		if (session != null)
			session.close();
		return result;
	}

	public ReturnResult getNoteBookGroup(HttpServletRequest request) {
		LoginInfoCookie cookie =CommonBo.getInstance().checkLogin(request);
		ReturnResult result = new ReturnResult();
		if (cookie != null) {
			String userId = cookie.getUserId();
			SqlSession session = getSqlSession();
			DailyNotebookDao dnbDao = session.getMapper(DailyNotebookDao.class);
			DailyNotebookGroupDao dnbgDao = session.getMapper(DailyNotebookGroupDao.class);
			DailyNotebookGroup group = new DailyNotebookGroup();
			group.setUserId(userId);
			group.setStatus(1);
			List<DailyNotebookGroup> groups = dnbgDao.selectByModel(group);
			List<TreeModel> trees = new ArrayList<TreeModel>();
			for (DailyNotebookGroup g : groups) {
				TreeModel tree = new TreeModel();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("userId", userId);
				map.put("bookGroupId", g.getId());
				map.put("status", 1);
				List<DailyNotebook> notebooks = dnbDao.selectByMap(map);
				tree.setId(g.getId());
				tree.setAttributes("group");
				tree.setChecked(false);
				tree.setIconCls("icon-folder");
				tree.setState("open");
				tree.setText(g.getGroupTitle());
				List<TreeModel> trees2 = new ArrayList<TreeModel>();
				for (DailyNotebook b : notebooks) {
					TreeModel tree2 = new TreeModel();
					tree2.setId(b.getId());
					tree2.setAttributes("notebook");
					tree2.setChecked(false);
					tree2.setIconCls("icon-write");
					tree2.setState("open");
					tree2.setText(b.getBookName());
					trees2.add(tree2);
				}
				if (trees2.size() > 0) {
					tree.setChildren(trees2);
				}
				trees.add(tree);
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
			map.put("bookGroupId", 0);
			map.put("status", 1);
			List<DailyNotebook> notebooks = dnbDao.selectByMap(map);
			// List<TreeModel> trees2 = new ArrayList<TreeModel>();
			for (DailyNotebook b : notebooks) {
				TreeModel tree2 = new TreeModel();
				tree2.setId(b.getId());
				tree2.setAttributes("notebook");
				tree2.setChecked(false);
				tree2.setIconCls("icon-write");
				tree2.setState("open");
				tree2.setText(b.getBookName());
				trees.add(tree2);
			}
			result.setStatus(Status.SUCCESS);
			result.setRows(trees);
			session.close();
		} else {
			result.setStatus(Status.NOLOGIN);
			result.setMsg(i18nUtils.getVal("200004001"));
        }
		
		return result;
	}
}
